﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookStore.Admin.Models;
using System.Data;
namespace BookStore.Admin.Controllers
{
    [HandleError]
    public class CategoryController : Controller
    {
        public ActionResult Index()
        {
            if (Session["Session_Admin"] == null) return RedirectToAction("index", "login");

            CategoriesModel tbCategory = new CategoriesModel();
            string[][] result = tbCategory.getAllCategories();
            ViewData["allCate"] = result;

            return View();
        }

        public ActionResult jsonAddCategory()
        {
            if (Session["Session_Admin"] == null) return Content("-3");
            if (this.Request.QueryString["name"].Trim() == "") return Content("-2");
            CategoriesModel tbCategory = new CategoriesModel();
            int result = tbCategory.insertCategory(Server.HtmlEncode(this.Request.QueryString["name"].Trim()));

            if (result == 1) return Content(this.getAllCategory());
            else return Content(result.ToString());
        }

        public ActionResult jsonDeleteCategory()
        {
            if (Session["Session_Admin"] == null) return Content("-3");
            
            CategoriesModel tbCategory = new CategoriesModel();
            int result = tbCategory.deleteCategory(Convert.ToInt32(this.Request.QueryString["id"]));

            if (result == 1) return Content(this.getAllCategory());
            else return Content(result.ToString());
        }

        public ActionResult jsonEditCategory()
        {
            if (Session["Session_Admin"] == null) return Content("-3");
            if (this.Request.QueryString["name"].Trim() == "") return Content("-2");
            CategoriesModel tbCategory = new CategoriesModel();
            int result = tbCategory.editCategory(Convert.ToInt32(this.Request.QueryString["id"]),Server.HtmlEncode(this.Request.QueryString["name"].Trim()));

            if (result == 1) return Content(this.getAllCategory());
            else return Content(result.ToString());
        }

        public ActionResult deleteCategories()
        {
            if (Session["Session_Admin"] == null) return RedirectToAction("index", "login");

            CategoriesModel tbCategory = new CategoriesModel();

            if (this.Request.Form["selected[]"] != null)
            {
                foreach (string value in this.Request.Form["selected[]"].Split(','))
                    tbCategory.deleteCategory(Convert.ToInt32(value));
            }

            return RedirectToAction("index", "category");
        }

        private string getAllCategory()
        {
            CategoriesModel tbCategory = new CategoriesModel();
            string[][] allCate = tbCategory.getAllCategories();

            string html = "";
            if (allCate[0] != null)
            {
                foreach (string[] row in allCate)
                {
                    if (row == null) break;
                    html += "<tr>";
                    html += "<td style='text-align: center;'> ";
                    html += "    <input name='selected[]' value='" + row[0] + "' type='checkbox'>";
                    html += "</td>";
                    html += "<td class='left' id='" + row[0] + "'>" + row[1] + "</td>";
                    html += "<td class='right'>";
                    html += "    [ <a href='javascript:;' onclick='editCategory(" + row[0] + ",0)'>Sửa</a> ]";
                    html += "    [ <a href='javascript:;' onclick='deleteCategory(" + row[0] + ")'>Xóa</a> ]";
                    html += "</td>";
                    html += "</tr>";
                }
            }
            else
            {
                html += "<tr>";
                html += "<td colspan='3' height='40' align='center'>Không có danh mục sách nào ! </td>";
                html += "</tr> ";
            }
            return html;
        }
    }
}
